'--------------------------------------------------------'
' Description:
'   This script intends for printing pin's net name to its pad
'--------------------------------------------------------'
Option Explicit

' Add any type libraries to be used. 
Scripting.AddTypeLibrary("MGCPCB.ExpeditionPCBApplication")

CALL GenerateValue2UL

Function GenerateValue2UL()
    ' generate part values for top and bottom layer
	CALL GenerateContentLayer("Values_bot", epcbSideBottom)
    CALL GenerateContentLayer("Values_top", epcbSideTop)
End Function

Function GenerateContentLayer(userLayerName,side)
    Dim pcbDoc
    Set pcbDoc = RetainActiveDoc()
	pcbDoc.CurrentUnit =  EPcbUnit.epcbUnitMils
    ' Get User Layer
    ' If not Exist create it
    ' See if the user layer already exists.
    Dim usrLyrObj
    Set usrLyrObj = pcbDoc.FindUserLayer(userLayerName)
    If usrLyrObj Is Nothing Then
    Set usrLyrObj = pcbDoc.SetupParameter.PutUserLayer(userLayerName)
    Else
        ' It does exist, remove any gfx on the user layer
        pcbDoc.UserLayerTexts(, userLayerName).Delete
    End If

	' ensure user layer is on top of the view
	Dim displCtrlObj
	Set displCtrlObj = pcbDoc.ActiveViewEx.DisplayControl
	Dim dispCtrlGlobalObj
	Set dispCtrlGlobalObj = displCtrlObj.Global
	dispCtrlGlobalObj.UserLayerDisplayLevel(userLayerName) = epcbDisplayLevelTop
	dispCtrlGlobalObj.RaiseUserLayerDisplayLevel(userLayerName)

    ' Get the component collection
    Dim cmpsColl
    Set cmpsColl = pcbDoc.Components
    
    ' filter
    Dim i
    For i = cmpsColl.Count To 1 Step -1
    'Determine if this component is on the size.
    If Not cmpsColl.Item(i).Side = side Then  
        'Component is not on desired side so remove it 
        cmpsColl.Remove(i)
    End If
    Next

    ' Filter collection to remove any component not 
    ' found on bottom side of board.
    Dim part
    Dim v
    'Dim vp
    'Dim pins
    'Dim pin
	Dim fontName
    Dim ch
    Dim pen
    pen = 2.0
	fontName = "vf_std"
	'ch = 40
	'Dim partOrientaion
	Dim textOrientation
	Dim textAttr
	textAttr = 0

	If side = epcbSideBottom Then
		textAttr = 4
	End If
	
    For Each part in cmpsColl
		'MsgBox "N:" & part.RefDes  & ",Net:" & pin.Net.Name & ",CX:" & cx & ",CY:" & cy
		set v = part.properties("Value")
		If v is Nothing Then
		'MsgBox "ERR,N:" & part.RefDes
		Else
		'MsgBox "ERR,N:" & part.RefDes  & ",V:" & v.Value
		On Error Resume Next
		ch = CalcFontHeightForPart(part, v.Value)
		'textOrientation = CalcFontOrientationForPin(pin)
		'MsgBox "ERR,N:" & part.RefDes  & ",V:" & v.Value & ",H:" & ch
		' ch for font height, pen for pen width
		' use CenterX instead of PositionX
		' textOrientation
        'If InStr(part.RefDes,"C01") > 0 Then
		'	MsgBox "N:" & part.RefDes  & ",CH:" & ch & ",C/P:" & (ch/pen)
		'End If
		If (ch/pen) < 5.0 Then
			pen = 1.0
		Else
			pen = 1.0
		End If
		Call pcbDoc.PutUserLayerText(v.Value,part.CenterX, part.CenterY, usrLyrObj,ch,part.Orientation,pen,fontName,textAttr,,,part)
		If Err.Number <> 0 Then
			'MsgBox "ERR,N:" & part.RefDes  & ",Net:" & pin.Net.Name & ",CH:" & ch & ",orientation:" & pin.Orientation
			'MsgBox "Src:" & Err.Source & ",Des:" & Err.Description
			Err.Clear
		End If
		'Call pin.PutUserLayerText(pin.Net.Name,pin.PositionX, pin.PositionY, epcbTextPinUserDefined, usrLyrObj)
		'Exit For
		End If
    Next
End Function

Function CalcFontHeightForPart(part, value)
    'Dim pins
    'Dim pin
    Dim outline
	Dim geometry
    Dim cx
    Dim cy
    Dim ch
    Dim netnameSize
	netnameSize = Len(value)
    Dim finalCh
    finalCh = 100000
    'set pins = part.Pins
    'For Each pin in pins
        set outline = part.PlacementOutlines(1)
		set geometry = outline.Geometry
        cx = geometry.RectMaxX - geometry.RectMinX
        cy = geometry.RectMaxY - geometry.RectMinY
        'If InStr(part.RefDes,"R") > 0 Then
		'	MsgBox "N:" & part.RefDes  & ",CX:" & cx & ",CY:" & cy
		'End If
        ch = cy
        If cx > cy Then
            ch = cx
        End If
        ch = (ch / netnameSize)
        If (finalCh > ch) Then
            finalCh = ch
        End If
    'Next

    'If finalCh < 1 Then
    '    finalCh = 6
    'End If

    CalcFontHeightForPart = finalCh
End Function

Function RetainActiveDoc()
' Get the application object 
    Dim pcbApp
    Set pcbApp = Application

    ' Get the active document 
    Dim pcbDoc
    Set pcbDoc = pcbApp.ActiveDocument

    ' License the document 
    ValidateServer(pcbDoc)

    Set RetainActiveDoc = pcbDoc
End Function

' Server validation function
Function ValidateServer(docObj)
  
  Dim keyInt
  Dim licenseTokenInt
  Dim licenseServerObj

  ' Ask Expedition’s document for the key
  keyInt = docObj.Validate(0)

  ' Get license server
  Set licenseServerObj = CreateObject("MGCPCBAutomationLicensing.Application")

  ' Ask the license server for the license token
  licenseTokenInt = licenseServerObj.GetToken(keyInt)

  ' Release license server
  Set licenseServerObj = nothing

  ' Turn off error messages (validate may fail if the token is incorrect)
  On Error Resume Next
  Err.Clear

  ' Ask the document to validate the license token
  docObj.Validate(licenseTokenInt)
  If Err Then
	  ValidateServer = 0    
  Else
	  ValidateServer = 1
  End If

End Function
